

var json = JSON.parse(document.cookie);
var user = json.user;
var pwd = json.pwd;

var db_now = "/"
var data_name = "/"
var db_api = new DBAPI();
db_api.set_login(user, pwd);

function show_fix_key(key_name) {
    document.getElementById("fix_key").style.display = "block";
    document.getElementById("key_name").innerText = key_name;
    

    db_api.get_key_value_from_database(data_name, key_name, db_now, function(data) {
        console.log(data);
        var value = data["message"];
        document.getElementById("value_now").innerText = value;
    });
}

function save_fix_key(key_name) {
    console.log(key_name);
    var value = document.getElementById("value_create").value;

    db_api.update_key_value_in_database(data_name, key_name , value, db_now, function(data) {
        console.log(data);
        alert(data['message']);
        document.getElementById("fix_key").style.display = "none";
    });
}

function search_data() {
    if (db_now == "/") {
        alert("请先选择数据库");
        return;

    }
    var search_str = prompt("请输入要搜索的数据：");
    if (search_str == null) {
        return;
    }
    document.getElementById("data_list").innerHTML = "";
    db_api.list_databases(db_now, function(data) {
        console.log(data);
        var db_list = data["message"];
        for(var i=0; i<db_list.length; i++) {
            (function(db_list) {
                if (db_list[i] == "") {
                    return;
                }
                if (String(db_list[i]).indexOf(search_str) != -1) {
                    var option = document.createElement("button");
                    option.innerText = db_list[i];
                    option.value = db_list[i];
                    option.className = "button_1";
                    document.getElementById("data_list").appendChild(option);
                    option.onclick = function() {
                        db_now = this.value;
                        document.getElementById('name').innerText = "所在数据库名称: "+ this.value;
                        load_db_list(db_now);
                    }
                }
            })(db_list);
        }
    });
}

function load_data_list(data_name) {
    document.getElementById("data_list").innerHTML = "";
    db_api.show_data_keys_from_database(data_name,db_now, function(data) {
        console.log(data);
        var data_list = data["message"];
        for(var i=0; i<data_list.length; i++) {
            (function(data_list) {
                if (data_list[i] == "") {
                    return;
                }
                var option = document.createElement("button");
                option.innerText = data_list[i];
                option.value = data_list[i];
                option.className = "button_1";
                document.getElementById("data_list").appendChild(option);
                option.onclick = function() {
                    show_fix_key(this.value);
                }
            })(data_list);
        }
    });
}

function delete_data() {
    if (data_name == "/") {
        alert("请先选择数据表");
        return;
    }
    var prompt_text = confirm("确认删除当前数据表吗？");
    if (prompt_text == true) {
        db_api.delete_data_from_database(data_name,db_now, function(data) {
            console.log(data);
            alert(data['message']);
            data_name = "/";
            load_db_list(db_now);
        });
    }
}

function delete_key_to_data(key_name) {
    if (data_name == "/") {
        alert("请先选择数据表");
        return;
    }
    var prompt_text = confirm("确认删除当前键值对吗？");
    if (prompt_text == true) {
        db_api.delete_key_value_from_database(data_name,key_name,db_now, function(data) {
            console.log(data);
            alert(data['message']);
            load_data_list(data_name);
            document.getElementById("fix_key").style.display = "none";
        });
    }
}

function search_db(){
    var search_str = prompt("请输入要搜索的数据库名称：");
    if (search_str == null) {
        return;
    }
    db_api.search_databasename_from_database(search_str, function(data) {
        console.log(data);
        var db_list = data["message"];
        document.getElementById("data_list").innerHTML = "";
        for(var i=0; i<db_list.length; i++) {
            (function(db_list) {
                if (db_list[i] == "") {
                    return;
                }
                var option = document.createElement("button");
                option.innerText = db_list[i];
                option.value = db_list[i];
                option.className = "button_1";
                document.getElementById("data_list").appendChild(option);
                option.onclick = function() {
                    db_now = this.value;
                    document.getElementById('name').innerText = "所在数据库名称: "+ this.value;
                    load_db_list(db_now);
                }
            })(db_list);
        }
    });
}

function delete_current_db(){
    if (db_now == "/") {
        alert("请先选择数据库");
        return
    }
    var prompt_text = confirm("确认删除当前数据库吗？");
    if (prompt_text == true) {
        db_api.delete_database(db_now, function(data) {
            console.log(data);
            alert(data['message']);
            show_databases();
        });
    }
}

function load_db_list(db_name) {
    document.getElementById("data_list").innerHTML = "";
    db_api.list_databases(db_name, function(data) {

        console.log(data);
        var db_list = data["message"];


        for(var i=0; i<db_list.length; i++) {
            (function(db_list) {
                if (db_list[i] == "") {
                    return;
                }
                var option = document.createElement("button");
                option.innerText = db_list[i];
                option.value = db_list[i];
                option.className = "button_1";
                document.getElementById("data_list").appendChild(option);
                option.onclick = function() {
                    data_name = this.value;
                    
                    document.getElementById('data_name').innerText = "当前所选数据名称: "+ this.value;
                    load_data_list(data_name);
                }
            })(db_list);
        }
    });
}

function back_select_db() {
    if(db_now == "/") {
        return
    }
    load_db_list(db_now);
}

function create_new_db() {
    var name = prompt("请输入新的数据库名称：");
    if (name == null) {
        return;
    }
    db_api.create_database(name, function(data) {
        console.log(data);
        alert(data['message'])
    });
}

function show_databases() {
    db_api.show_databases(function(data) {
        console.log(data);
        var db_list = data["message"];
        document.getElementById("data_list").innerHTML = "";
        for(var i=0; i<db_list.length; i++) {
            (function(db_list) {
                if (db_list[i] == "") {
                    return;
                }
                var option = document.createElement("button");
                option.innerText = db_list[i];
                option.value = db_list[i];
                option.className = "button_1";
                document.getElementById("data_list").appendChild(option);
                option.onclick = function() {
                    db_now = this.value;
                    document.getElementById('name').innerText = "所在数据库名称: "+ this.value;
                    load_db_list(db_now);
                }
            })(db_list);
        }
    });
    db_now = "/";
    document.getElementById('name').innerText = "所在数据库名称: "+ db_now;
}

function create_data_to_db() {
    var data_name = document.getElementById("data_name_create").value;
    // 获取 key_value_list 元素下所有的输入框的value
    var key_value_list = document.getElementById("key_value_list");
    var key_value_list_items = key_value_list.getElementsByTagName("input");
    var key_value_list_data = "";
    console.log(key_value_list_items)
    for (var i = 0; i < key_value_list_items.length; i+=2) {
        var key = key_value_list_items[i].value;
        var value = key_value_list_items[i+1].value;
        key_value_list_data += key + "=" + value + ",";
    }
    console.log(key_value_list_data);
    db_api.insert_data_in_database(data_name,db_now, key_value_list_data, function(data) {
        console.log(data);
        alert(data['message']);
        document.getElementById("create_data").style.display = "none";
        load_db_list(db_now);
    });
}

function create_key_to_data() {
        // 获取 key_value_list 元素下所有的输入框的value
        var key_value_list = document.getElementById("key_value_list_1");
        var key_value_list_items = key_value_list.getElementsByTagName("input");
        var key_value_list_data = "";
        console.log(key_value_list_items)
        for (var i = 0; i < key_value_list_items.length; i+=2) {
            var key = key_value_list_items[i].value;
            var value = key_value_list_items[i+1].value;
            key_value_list_data += key + "=" + value + ",";
        }
        console.log(key_value_list_data);
        db_api.create_key_to_database(data_name,key_value_list_data,db_now, function(data) {
            console.log(data);
            alert(data['message']);
            document.getElementById("create_data").style.display = "none";
            load_db_list(db_now);
        });
}

function show_create_key() {
    if (db_now == "/") {
        alert("请先选择数据库");
        return;
    }
    else if (data_name == "/")
    {
        alert("请先选择数据表");
        return;

    }
    document.getElementById("create_key").style.display = "block";
    document.getElementById("data_name_create_1").innerText = data_name;
}

function show_create_data() {
    if (db_now == "/") {
        alert("请先选择数据库");
        return;
    }
    document.getElementById("create_data").style.display = "block";
    document.getElementById("db_name_create").innerText = db_now;
}

window.onload = function() {
    // 尝试新建数据库，不管存不存在都返回成功
    var xhr = new XMLHttpRequest();
    xhr.open("GET" , "https://api.hackerstack.top/create_db/"+user+"/"+pwd, true);
    xhr.send(null);
    xhr.onload = function() {
        console.log(xhr.responseText);
        show_databases();
    }
}